package com.sunlands.zlcx.usercenter.service.status;

import com.sunlands.zlcx.usercenter.constant.LearningStatus;
import com.sunlands.zlcx.usercenter.domain.UsersStudyInfoDO;
import com.sunlands.zlcx.usercenter.domain.UsersTargetRelationDO;
import com.sunlands.zlcx.usercenter.exception.CheckParamException;
import com.sunlands.zlcx.usercenter.service.status.common.AbstractStudyStatus;
import com.sunlands.zlcx.usercenter.vo.UsersStudyInfoVO;
import com.sunlands.zlcx.usercenter.vo.UsersVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutionException;

import static com.sunlands.zlcx.usercenter.config.RedisConfig.CacheKeys.USER_TARGET_BY_USERID;

/**
 * @author gaoyanfeng
 * @create 2019-04-26 18:07
 **/
@Component
@Slf4j
public class JobCountTime extends AbstractStudyStatus {
    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = USER_TARGET_BY_USERID, key = "#usersStudyInfoVO.userId", condition = "#usersStudyInfoVO.userId != null"),
    })
    public void handle(UsersStudyInfoVO usersStudyInfoVO, UsersTargetRelationDO usersTargetRelationDO) {
        Long userId = usersTargetRelationDO.getUserId();
        UsersVO usersVO = usersService.findOne(userId);
            usersStudyInfoVO.setType(UsersStudyInfoDO.TypeEnums.ORDERNARY_COUNT_TIME.code);
            LearningStatus learningStatus = LearningStatus.getLearningStatus(usersVO.getStatus());
            if (learningStatus != LearningStatus.LEARN_TIMING && learningStatus !=LearningStatus.LEARN_TIMING_PAUSE && learningStatus != LearningStatus.LEARN_TIMING_CONINUE) {
                //用户没有在学习中
                return;
            }
            long endTime = usersStudyInfoVO.getEndTime().getTime();
            long startTime = usersVO.getStartLearnTime().getTime();
            Long studyTime = endTime - startTime;
            log.info("timeDiff={}", studyTime);
            if (endTime < startTime) {
                throw new IllegalArgumentException("bad heart ,you can to shi");
            }
            usersStudyInfoVO.setStudyMinuteSeconds(studyTime.intValue() /1000);
            log.info("OrdernaryCountTime userid={}， studyTime.intValue()/1000={}", userId, studyTime.intValue()/1000);
            learnTimingProcess(usersStudyInfoVO, usersTargetRelationDO, userId);


    }
}
